#include "shared.h"
#include "serial.h"
#include "buffers.h"
#include "string.h"

#ifdef useFlowControl
volatile unsigned char XOFFsent = 0;
#endif

char putcUSARTInt(unsigned char data){
	PIE1bits.TXIE = 0;
	if(sendCount >= sendBufSize){
		return -1;
	}
	sendBuffer[sendCount++] = data;
	PIE1bits.TXIE = 1;
	return 0;
}

char putsUSARTInt(const unsigned char * data){
	unsigned char len = strlen(data);
	if(!len || len >= sendBufSize)
		return -1;
	while(PIE1bits.TXIE);
	sendCount = len;
	memcpy(sendBuffer, data, sendCount);
	PIE1bits.TXIE = 1;
	return 0;
}

char putrsUSARTInt(const rom unsigned char * data){
	unsigned char len = strlenpgm(data);
	if(!len || len >= sendBufSize)
		return -1;
	while(PIE1bits.TXIE);
	sendCount = len;
	memcpypgm2ram(sendBuffer, data, sendCount);
	PIE1bits.TXIE = 1;
	return 0;
}

char putsnUSARTInt(const unsigned char * data, unsigned char len){
	if(len > sendBufSize)
		return -1;
	if(!(INTCON & 0xc0))
		INTCON |= 0xc0;
	while(PIE1bits.TXIE && (INTCON |= 0xc0));
	sendCount = len;
	memcpy(sendBuffer, data, sendCount);
	PIE1bits.TXIE = 1;
	return 0;
}

char putrsnUSARTInt(const rom unsigned char * data, unsigned char len){
	if(len > sendBufSize)
		return -1;
	if(!(INTCON & 0xc0))
		INTCON |= 0xc0;
	while(PIE1bits.TXIE);
	sendCount = len;
	memcpypgm2ram(sendBuffer, data, sendCount);
	PIE1bits.TXIE = 1;
	return 0;
}

char CRLF()
{
	if(sendCount + 2 >= sendBufSize){
		return -1;
	}
	PIE1bits.TXIE = 0;
	sendBuffer[sendCount++] = 0xd;
	sendBuffer[sendCount++] = 0xa;
	PIE1bits.TXIE = 1;
	return 0;
}
